package com.egao.common.module.system.transfer;

import com.egao.common.core.web.JsonResult;
import com.egao.common.module.cms.entity.Category;
import com.egao.common.module.cms.service.CategoryService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 栏目数据迁移
 *
 * @author pyx
 */
@Api(tags = "栏目数据迁移")
@RestController
@Slf4j
public class CategoryDataController {
    @Value("${old.datasource.jyurl}")
    private String url;

    @Value("${old.datasource.username}")
    private String userName;

    @Value("${old.datasource.password}")
    private String password;

    @Autowired
    private CategoryService categoryService;

    public JsonResult category() {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;

        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(url, userName, password);
            st = conn.createStatement();
            String sql = "SELECT * FROM articlecategory";
            rs = st.executeQuery(sql);

            while (rs.next()){
                Category category = new Category();
                category.setOldId(rs.getString("id"));
                String parentId = rs.getString("parent_id").replace(",","");
                category.setOldParentId(parentId);
                category.setName(rs.getString("name"));
                category.setSort(rs.getInt("sort"));
                category.setCreateTime(rs.getDate("create_date"));
                category.setUpdateTime(rs.getDate("update_date"));
                category.setLinkUrl(rs.getString("remarks"));

                int categoryType = rs.getInt("category_type");
                String type = null;
                if(categoryType == 1){
                    type = "1";
                }else if(categoryType == 2){
                    type = "3";
                }else if(categoryType == 3){
                    type = "2";
                }

                category.setType(type);

                String show = rs.getString("defaultshow");
                String homeFlag = "0";
                if("5".equals(show)){
                    homeFlag = "1";
                }

                category.setHomeFlag(homeFlag);

                String delFlag = rs.getString("del_flag");
                int del = 0;
                if("1".equals(delFlag)){
                    del = 1;
                }
                category.setDeleted(del);

                try {
                    categoryService.save(category);
                }catch (Exception e){
                    log.error("插入id为: {} 的数据出现异常",rs.getString("id"));
                    break;
                }
            }

            matching();

        }catch (ClassNotFoundException | SQLException e){
            e.printStackTrace();
        }finally {
            try {
                rs.close();
                st.close();
                conn.close();
                return JsonResult.ok();
            }catch (SQLException e){
                e.printStackTrace();
                return JsonResult.error();
            }
        }
    }

    /**
     * 根据原数据的id,和parentId,匹配新id和parentid
     */
    public void matching(){
        List<Category> oldList = categoryService.oldCategoryList();

        if(oldList != null || oldList.size() > 0){

            List<Category> listCategory = new ArrayList<>();
            listCategory.addAll(oldList);

            for (Category old:oldList) {
                if("0".equals(old.getOldParentId())){
                    old.setParentId(0);
                }else{
                    for (Category category:oldList) {
                        if(old.getOldParentId().equals(category.getOldId())){
                            old.setParentId(category.getId());
                        }
                    }
                }
            }

            categoryService.updateBatchById(oldList);
        }
    }
}
